﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.membercenter.order
{
    public partial class order_center : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["MemberName"] != null)
                {
                    string memberId = Session["MemberName"].ToString();
                    ViewState["menberId"] = memberId;
                    this.litlist.Text=GetListById(memberId);
                }
            }
           
        }
        public string GetListById(string userId)
        {
            YXShop.BLL.Order.Orders bll = new YXShop.BLL.Order.Orders();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.Common.OrdersStatusEnum ose = new YXShop.Common.OrdersStatusEnum();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetListByPage(" and UserId = '" + userId+"' ");
            int index = 0;
            table.AddHeadCol("","序号");
            table.AddHeadCol("","订单号");
            table.AddHeadCol("","下单时间");
            table.AddHeadCol("","订单金额");
            table.AddHeadCol("","收款金额");
            //table.AddHeadCol("","需要发票");
           // table.AddHeadCol("","已开发票");
            table.AddHeadCol("","订单状态");
            table.AddHeadCol("","付款状态");
            table.AddHeadCol("","物流状态");
            table.AddRow();
            if(dataPage.DataReader!=null)
            {
                while(dataPage.DataReader.Read())
                {
                    index++;
                    table.AddCol((dataPage.PageCount*(dataPage.PageIndex-1)+index).ToString());
                    table.AddCol("<a href=\"order_info.aspx?OrderId=" + dataPage.DataReader["Id"].ToString() + "\">" + dataPage.DataReader["OrderId"].ToString() + "</a>");
                    table.AddCol(dataPage.DataReader["OrderDate"].ToString());
                    table.AddCol(Convert.ToDouble(dataPage.DataReader["FactPrice"].ToString()).ToString("f2"));
                    table.AddCol(Endpayment(dataPage.DataReader["OrderId"].ToString()));
                    //table.AddCol(dataPage.DataReader["Invoice"].ToString() == "0" ? "×" : "√");
                    table.AddCol(ose.OrderStatus(int.Parse(dataPage.DataReader["OrderStatus"].ToString())));
                    table.AddCol(ose.PaymentStatus(int.Parse(dataPage.DataReader["PaymentStatus"].ToString())));
                    table.AddCol(ose.OgisticsStatu(int.Parse(dataPage.DataReader["OgisticsStatus"].ToString())));
                    table.AddRow();
                }

            }
            string test = string.Empty;
            test = table.GetTable()+dataPage.PageToolBar;
            table = null;
            dataPage.Dispose();
            dataPage = null;
            bll = null;
            return test;
        }
        #region 已付款
        public string Endpayment(string orderId)
        {
            string totle = string.Empty;
            decimal remPrice = 0;
            decimal payPrice = 0;
            decimal prePrice = 0;
            //银行支付
            YXShop.BLL.Order.RemittanceInfo remBll = new YXShop.BLL.Order.RemittanceInfo();
            List<YXShop.Model.Order.RemittanceInfo> remdate = remBll.GetAll("orderid='" + orderId + "'");
            if (remdate.Count > 0)
            {
                foreach (YXShop.Model.Order.RemittanceInfo da in remdate)
                {
                    remPrice += Convert.ToDecimal(da.RemittanceMoney.ToString());
                }
            }
            //现金支付
            YXShop.BLL.Order.PaymentMoney payBll = new YXShop.BLL.Order.PaymentMoney();
            List<YXShop.Model.Order.PaymentMoney> paydata = payBll.GetAll("orderid='" + orderId + "'");
            if (paydata.Count > 0)
            {
                foreach (YXShop.Model.Order.PaymentMoney da in paydata)
                {
                    payPrice += Convert.ToDecimal(da.GatheringMoney.ToString());
                }
            }
            //预付款
            YXShop.BLL.Order.PrepayMoney preBll = new YXShop.BLL.Order.PrepayMoney();
            List<YXShop.Model.Order.PrepayMoney> predata = preBll.GetAll("orderid='" + orderId + "'");
            if (predata.Count > 0)
            {
                foreach (YXShop.Model.Order.PrepayMoney da in predata)
                {
                    prePrice += Convert.ToDecimal(da.PayoutMoney);
                }
            }
            totle = (remPrice + payPrice + prePrice).ToString();
            return totle;
        }
        #endregion

        #region 计算商品总价
        private double TotalPrice(string orderId)
        {
            YXShop.BLL.Order.OrderProduct bll = new YXShop.BLL.Order.OrderProduct();
            DataTable dt = bll.GetListOrderProduct(orderId);
            double totalPrice = 0;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    totalPrice += Convert.ToDouble(dt.Rows[i]["ProPrice"].ToString()) * Convert.ToDouble(dt.Rows[i]["ProNum"].ToString());
                }
            }
            return totalPrice;
        }
        #endregion
        #region 发票
        public string Estimate(object obj)
        {
            return "";
        }
        #endregion

        protected void linkSelect_Click(object sender, EventArgs e)
        {
            this.litlist.Text = GetListById(ViewState["menberId"].ToString());
        }
    }
}
